package com.my.lab;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Vibrator;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class SliderRelativeLayout extends RelativeLayout {

	private static String TAG = "SliderRelativeLayout";
	private Context mContext = null;
	private Bitmap bitmap = null;
	private ImageView icon = null;
	private int mLastMoveX = 1000;
	private int mLastMoveY = 1000;
	
	private int retainX = 0;
	private int retainY = 0;
	
	public SliderRelativeLayout(Context context) {
		super(context);
		mContext = context;
		initDragBitmap();  
	}
	
	public SliderRelativeLayout(Context context, AttributeSet attrs) {
		super(context, attrs, 0);
		mContext = context;
		initDragBitmap();
	}

	public SliderRelativeLayout(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
		initDragBitmap();
	}
	
	
	private void initDragBitmap() {
		if(bitmap == null)
			bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.new_sms_button_bg_a);
	}
	
	@Override
	protected void onFinishInflate() {
		Log.d(TAG, "onFinishInflate>>>");
		super.onFinishInflate();
		icon = (ImageView) findViewById(R.id.radius);
		retainX = icon.getWidth();
		retainY = icon.getHeight();
		
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		int x = (int)event.getX();
		int y = (int)event.getY();
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			Log.d(TAG, "MotionEvent.ACTION_DOWN");
			mLastMoveX = x ;
			return handleActionDownEvenet(event); 
		case MotionEvent.ACTION_MOVE:
			Log.d(TAG, "MotionEvent.ACTION_MOVE");
			mLastMoveX = x ;
			mLastMoveY = y;
			invalidate();
			return true;
		case MotionEvent.ACTION_UP:
			Log.d(TAG, "MotionEvent.ACTION_UP");
			handleActionUpEvent(event); 
			return true;
		}
		return super.onTouchEvent(event);
	}
	
	private void handleActionUpEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		
	}
	
	
	private boolean handleActionDownEvenet(MotionEvent event) {
		Rect rect = new Rect();
		icon.getHitRect(rect);
		boolean hit = rect.contains((int)event.getX(), (int)event.getY());
		if(hit){
			Log.d(TAG, "handleActionDownEvenet>>>hit true");
//			icon.setVisibility(View.INVISIBLE);
			virbate();
		}
		return hit;
	}
	
	public void onDraw(Canvas canvas) {
		Log.d(TAG, "onDraw");
		super.onDraw(canvas);
		invalidateDragImg(canvas);  
	}
	
	private void invalidateDragImg(Canvas canvas) {
		int drawX = mLastMoveX - bitmap.getWidth();
		int drawY = mLastMoveY - bitmap.getHeight();
		canvas.drawBitmap(bitmap, drawX, drawY, null);
		Log.d(TAG, "invalidateDragImg>>>drawX:"+drawX+",drawY:"+drawY);
	}
	
	private void virbate(){
		Vibrator vibrator =(Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
		vibrator.vibrate(200);
	}
}
